package org.tinygroup.dbf.impl;

import org.tinygroup.dbf.DbfDataSet;
import org.tinygroup.dbf.DbfRecord;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Created by luoguo on 2014/7/10.
 * 
 */
public class MemoryDbfDataSet implements DbfDataSet {
    private List<DbfRecord> dbfRecords = new ArrayList<DbfRecord>();
    private int index = -1;

    public List<DbfRecord> getDbfRecords() {
        return dbfRecords;
    }

    public void setDbfRecords(List<DbfRecord> dbfRecords) {
        this.dbfRecords = dbfRecords;
    }

    public void moveBeforeFirst() throws IOException {
        index = -1;
    }

    public boolean next() {
        if (index + 1 < dbfRecords.size()) {
            index++;
            return true;
        }
        return false;
    }

    public boolean absolute(int position) {
        if (position >= 1 && position <= dbfRecords.size()) {
            index = position - 1;
            return true;
        }
        return false;
    }

    public DbfRecord getDbfRecord() throws IOException { 
        if (index < 0 || index >= dbfRecords.size()) {
            throw new IOException("当前记录位置为:" + index + ",不是合法的记录位置。");
        }
        return dbfRecords.get(index);
    }

	public int getDbfRecordCount() {
		return dbfRecords.size();
	}
}
